<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
      <div>
        <span>姓名:</span>
        <input type="text" v-model="name"/>
      </div>
      <div>
        <span>年龄:</span>
        <input type="number" v-model="age" />
      </div>
      <div>
        <span>性别:</span>
        <select v-model="genter" >
          <option value="男" >男</option>
          <option value="女" >女</option>
        </select>
      </div>
      <div>
        <button v-on:click="add">添加/修改</button>
      </div>
      <div>
        <table border="1" cellpadding="10" cellspacing="0">
          <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item,index) in list" :key="item.id">
            <td>{{index+1}}</td>
            <td>{{ item.name}}</td>
            <td>{{ item.age}}</td>
            <td>{{ item.genter}}</td>
            <td>
              <button v-on:click="delata(item.id)">删除</button>
              <button v-on:click="bianji(item.id)">编辑</button>
            </td>
          </tr>
        </table>
      </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
       list:[{id:1,name:'小华',age:'18',genter:'男'}],
       name:'',
       genter:'男',
       age:''
      },
      methods: {
        add(){
          if(this.name.trim() == '' || this.age.trim() == '') return alert('数据不能为空')
          this.list.push({
        id:Date.now(),
        name:this.name,
        age:this.age,
        genter:this.genter
        })
        this.age = '',
        this.name = '',
        this.genter = '男'
        },
        delata(id){
         this.list =   this.list.filter((item)=>{
            return item.id != id
          })
        },
        bianji(id){
         const obj =  this.list.filter((item)=>{
          return item.id == id
          })
          this.name = obj[0].name
          this.age = obj[0].age
          this.genter = obj[0].genter
        }
      },
    })
  </script>
</body>

</html>